package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Options;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.MatchScenesInfo;
import com.iot.ic.model.RefereeInfo;
import com.iot.ic.model.ScenesResult;
import com.iot.ic.model.StadiumInfo;
import com.iot.ic.model.TeamInfo;
import com.iot.ic.service.MatchScenesService;
import com.iot.ic.service.RefereeService;
import com.iot.ic.service.ScenesResultService;
import com.iot.ic.service.StadiumScenesService;
import com.iot.ic.service.TeamService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Api(description = "场景信息相关接口")
@RestController
public class ScenesController extends BaseController {

    @Autowired
    private MatchScenesService matchScenesService;

    @Autowired
    private StadiumScenesService stadiumScenesService;
    /**
     * 根据赛场Id获得场景Id集合
     *
     * @param userId    用户Id
     * @param stadiumId 赛道Id
     * @return
     */
    @ApiOperation(value = "根据赛场Id获得场景Id集合", notes = "")
    @ResponseBody
    @RequestMapping(value = "/users/{userId}/stadium/{stadiumId}/scenesIds", method = RequestMethod.GET)
    public Response listScenesIds(@PathVariable("userId") int userId,
                                  @PathVariable("stadiumId") int stadiumId) {
        Response response = new Response();
        List<Integer> stadiumScenesIdList = stadiumScenesService.listStadiumScenesIdsByStadium(stadiumId);
        response.setResult(stadiumScenesIdList);
        return response;
    }


    /**
     * 获得每个场景的信息
     *
     * @param userId
     * @param
     * @return
     */
    @ApiOperation(value = "获得每个场景的信息", notes = "")
    @ResponseBody
    @RequestMapping(value = "/users/{userId}/stadiumResult/{stadiumResultId}/scenes/{stadiumScenesId}/start", method = RequestMethod.GET)
    public Response getScenesById(@PathVariable("userId") int userId, @PathVariable("stadiumResultId") int stadiumResultId,
                                  @PathVariable("stadiumScenesId") int stadiumScenesId) {
        Response response = new Response();
        MatchScenesInfo matchScenesInfo = matchScenesService.startMatchScenes(stadiumScenesId, stadiumResultId);
        response.setResult(matchScenesInfo);
        return response;
    }

    @ApiOperation(value = "增加场景信息", notes = "")
    @PostMapping(value = "/users/{userId}/scenes")
    public Response addMatchScenes(@PathVariable(value = "userId") Integer userId, @RequestBody @Valid MatchScenesInfo matchScenesInfo) {
        Response response = new Response();
        matchScenesInfo.setCreateTime(new Date());
        matchScenesInfo.setDelFlag(Options.NotDel.getCode());
        //matchScenesInfo.setScenesScore(0);
        matchScenesInfo.setIllegalCount(0);
		int count = matchScenesService.checkNameExist(matchScenesInfo);
		if(count == 0){
			int result = matchScenesService.addMatchScenes(matchScenesInfo);
			if (result == 1)
				response.setInfos(ReturnCode.No_AddScenesOK);
			else
				response.setInfos(ReturnCode.Err_AddScenesFailor);
		}else{
			response.setInfos(ReturnCode.Err_AddScenesNameExist);
		}

        return response;
    }

	 @ApiOperation(value = "根据Id获得场景信息", notes = "")
	 @ResponseBody
	 @RequestMapping(value="/users/{userId}/scenes/{scenesId}", method=RequestMethod.GET)
	 public Response getMatchScenesById(@PathVariable(value = "userId") Integer userId,
			 @PathVariable(value = "scenesId") Integer scenesId ) {
	     Response response=new Response();
	     MatchScenesInfo matchScenesInfo = matchScenesService.getMatchScenesById(scenesId);
	     response.setResult(matchScenesInfo);
	     return response;
	 }
	
	 @ApiOperation(value = "更新场景信息", notes = "")
	 @ResponseBody
	 @RequestMapping(value="/users/{userId}/scenes/{scenesId}", method=RequestMethod.PUT)
	 public Response updateMatchScenes(@PathVariable(value = "userId") Integer userId,
			 @PathVariable(value = "scenesId") Integer scenesId,
			 @RequestBody @Valid MatchScenesInfo matchScenesInfo) {
	     Response response=new Response();
	     matchScenesInfo.setScenesId(scenesId);
	     int result = matchScenesService.updateMatchScenes(matchScenesInfo);
	     if (result==1)
	         response.setInfos(ReturnCode.No_UpdateScenesOK);
	     else
	         response.setInfos(ReturnCode.Err_UpdateScenesFailor);
	     return response;
	 }
	
	 @ApiOperation(value = "删除场景信息", notes = "")
	 @ResponseBody
	 @RequestMapping(value="/users/{userId}/scenes/{scenesId}", method=RequestMethod.DELETE)
	 public Response deleteMatchScenes(@PathVariable(value = "userId") Integer userId,
			 @PathVariable(value = "scenesId") Integer scenesId) {
	     Response response=new Response();
	     int result = matchScenesService.deleteMatchScenes(scenesId);
	     if (result==1)
	         response.setInfos(ReturnCode.No_DeleteScenesOK);
	     else
	         response.setInfos(ReturnCode.Err_DeleteScenesFailor);
	     return response;
	 }
	 
	 @ApiOperation(value = "场景排序", notes = "")
	 @ResponseBody
	 @RequestMapping(value="/users/{userId}/scenes/sort", method=RequestMethod.POST)
	 public Response sortMatchScenes(@PathVariable(value = "userId") Integer userId,
			 @RequestParam(value="stadiumScenesIds", required=false) String stadiumScenesIds) {
	     Response response = new Response();
	     int result = stadiumScenesService.sortScenes(stadiumScenesIds);
	     if (result==1)
	         response.setInfos(ReturnCode.No_Err);
	     else
	         response.setInfos(ReturnCode.Err_RequestFailed);
	   
	     return response;
	 }
	 
	 @ApiOperation(value = "根据赛场ID获得未绑定的场景列表", notes = "")
	 @ResponseBody
	 @RequestMapping(value="/users/{userId}/stadium/{stadiumId}/scenes", method=RequestMethod.GET)
	 public Response listUnboundMatchScenes(@PathVariable(value = "userId") Integer userId,
			 @PathVariable(value = "stadiumId") Integer stadiumId) {
	     Response response = new Response();
	     List<MatchScenesInfo> matchScenesList = matchScenesService.listUnboundScenesByStadium(stadiumId);
	     response.setResult(matchScenesList);
	     return response;
	 }
	 
	 @ApiOperation(value = "解除场景与赛场绑定", notes = "")
	 @ResponseBody
	 @RequestMapping(value="/users/{userId}/scenes/unbound", method=RequestMethod.DELETE)
	 public Response unboundMatchScenes(@PathVariable(value = "userId") Integer userId,
		 @RequestParam(value="stadiumScenesIds", required=false) String stadiumScenesIds) {
	     Response response = new Response();
	     int result = stadiumScenesService.unboundMatchScenes(stadiumScenesIds);
	     if (result==1)
	         response.setInfos(ReturnCode.No_Err);
	     else
	         response.setInfos(ReturnCode.Err_RequestFailed);
	     return response;
	 }
	 
	 
	 @ApiOperation(value = "获得场景列表", notes = "")
	 @ResponseBody
	 @RequestMapping(value="/users/{userId}/scenes/all", method=RequestMethod.GET)
	 public Response listAllMatchScenes(@PathVariable(value = "userId") Integer userId) {
	     Response response = new Response();
	     MatchScenesInfo matchScenesInfo = new MatchScenesInfo();
	     List<MatchScenesInfo> matchScenesList = matchScenesService.listAllScenes(matchScenesInfo);
	     response.setResult(matchScenesList);
	     return response;
	 }

	@ApiOperation(value = "分页获得场景列表", notes = "")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/scenes/page", method=RequestMethod.GET)
	public Response listMatchScenesByPage(
			@PathVariable(value = "userId") Integer userId,
			@RequestParam(value = "pageNo", required = false, defaultValue = "1")Integer pageNo,
			@RequestParam(value = "pageSize", required = false, defaultValue = "20")Integer pageSize
	) {
    	pageSize = pageSize < 1?20:pageSize;
		Response response = new Response();
		MatchScenesInfo matchScenesInfo = new MatchScenesInfo();
		List<MatchScenesInfo> matchScenesList = matchScenesService.pageScenes(matchScenesInfo, pageNo, pageSize);
		int total = matchScenesService.pageTotal(matchScenesInfo);
		response.setResult(matchScenesList);
		response.setTotalNum(total);
		return response;
	}
	 
	 
	 @ApiOperation(value = "获得场景详情", notes = "")
	 @ResponseBody
	 @RequestMapping(value="/users/{userId}/scenes/{scenesId}/detail", method=RequestMethod.GET)
	 public Response getDetailMatchScenes(@PathVariable(value = "userId") Integer userId,
			 @PathVariable(value = "scenesId") Integer scenesId) {
	     Response response = new Response();
	     MatchScenesInfo matchScenesInfo = matchScenesService.getMatchScenesInfoWithIllegal(scenesId);
	     response.setResult(matchScenesInfo);
	     return response;
	 }
	 
	 @ApiOperation(value = "获得设备详情", notes = "")
	 @ResponseBody
	 @RequestMapping(value="/users/{userId}/scenes/{scenesId}/detail1", method=RequestMethod.GET)
	 public Response getDetailMatchScenes1(@PathVariable(value = "userId") Integer userId,
			 @PathVariable(value = "scenesId") Integer scenesId) {
	     Response response = new Response();
	     MatchScenesInfo matchScenesInfo = matchScenesService.getMatchScenesInfoWithEquipment(scenesId);
	     response.setResult(matchScenesInfo);
	     return response;
	 }

	 
	
	 
	 
}
